/*
 * HideMe: hide files on a cellphone
 * Copyright (C) 2009  Eduard Martínez
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package hideme;

import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import org.netbeans.microedition.util.CancellableTask;

public class Hider implements CancellableTask {
    private final boolean hide;
    private final static String EXT = ".hideme";
    private final static int EXTL = EXT.length();
    private String failureMessage = "";
    private boolean hasFailed = false;
    private boolean cancel = false;

    public Hider(boolean hide){
        this.hide = hide;
    }

    public void run() {
        try{
            FileList f = new FileList();
            Enumeration e = f.elements();
            if(hide){
                while(e.hasMoreElements() && !cancel){
                    hide("file://"+(String) e.nextElement());
                }
            }else{
                while(e.hasMoreElements() && !cancel){
                    unhide("file://"+(String) e.nextElement());
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            failureMessage = e.getMessage();
            hasFailed = true;
        }
    }

    private void hide(String fileURL)throws Exception{
        //System.out.println("Hiding "+fileURL);
        Enumeration e = null;
        FileConnection fc = (FileConnection) Connector.open(fileURL);
        if(!fc.exists()){
            fc.close();
            //System.out.println(fileURL+" doesn't exist");
            return;
        }
        fc.setHidden(true);
        if(isDir(fileURL)){
            e = fc.list();
            //System.out.println(fileURL+" is directory!");
        }else{
            fc.rename(fc.getName()+EXT);
        }
        fc.close();
        if(e != null){
            //System.out.println("Going inside directory");
            while(e.hasMoreElements() && !cancel)
                hide(fileURL + (String)e.nextElement());
        }
    }

    private void unhide(String fileURL) throws Exception{
        //System.out.println("UnHiding "+fileURL);
        Enumeration e = null;
        if(!isDir(fileURL) && !fileURL.endsWith(EXT))
            fileURL = fileURL + EXT;
        FileConnection fc = (FileConnection) Connector.open(fileURL);
        if(!fc.exists()){
            fc.close();
            //System.out.println(fileURL+" doesn't exist");
            return;
        }
        fc.setHidden(false);
        if(isDir(fileURL)){
            //System.out.println(fileURL+" is directory!");
            e = fc.list("*", true);
        }else{
            fc.rename(fc.getName().substring(0, fc.getName().length() - EXTL));
        }
        fc.close();
        if(e != null){
            //System.out.println("Going inside directory");
            while(e.hasMoreElements() && !cancel)
                unhide(fileURL + (String)e.nextElement());
        }
    }

    private boolean isDir(String fileName){
        return fileName.endsWith("/");
    }

    public boolean cancel() {
        cancel = true;
        return true;
    }

    public boolean hasFailed() {
        return hasFailed;
    }

    public String getFailureMessage() {
        return failureMessage;
    }
}
